﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;

namespace SIMA.Core.Security
{
    [Serializable]
    public class Profile:IDisposable
    {

        #region Fields

        private string userName;
        private string ipAddress;
        private DateTime sessionInit;
        
        #endregion

        #region Properties

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public string IpAddress
        {
            get { return ipAddress; }
            set { ipAddress = value; }
        }

        public DateTime SessionInit
        {
            get { return sessionInit; }
            set { sessionInit = value; }
        }

        #endregion

        #region Events

        public event EventHandler BeginInit;
        public event EventHandler EndInit;

        #endregion

        #region Methods

        #region Private Methods

        private void Init()
        {
            if (BeginInit != null)
            {
                BeginInit(this, new System.EventArgs());
            }
            Thread.Sleep(500);
            GetIpFromLocalMachine();
            SessionInit = DateTime.Now;
            UserName = "Not Authorized";
        }

        private void GetIpFromLocalMachine()
        {
            string hostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(hostName);

            IPAddress[] addresses = ipEntry.AddressList;

            IpAddress = addresses[2].ToString();

        }

        #endregion

        #region Public Methods

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion        

        #endregion

        #region Constructor

        public Profile()
        {
            Init();
        }

        #endregion

        #region Destructor

        ~Profile()
        {
            this.Dispose();
        }

        #endregion

    }
}
